{% extends "base.html" %}

{% block title %}测控设备选型{% endblock %}

{% block content %}
<div class="container">
    <div class="row mb-4">
        <div class="col">
            <h2 class="mb-3">测控设备选型</h2>
            {% if current_user.role in ['engineer', 'manager'] %}
            <a href="{{ url_for('main.add_equipment') }}" class="btn btn-primary">
                <i class="fas fa-plus-circle me-1"></i>添加设备
            </a>
            {% endif %}
        </div>
    </div>

    <!-- 搜索表单 -->
    <div class="card mb-4">
        <div class="card-body">
            <form method="get" class="row g-3">
                <div class="col-md-4">
                    <div class="input-group">
                        <span class="input-group-text"><i class="fas fa-search"></i></span>
                        <input type="text" class="form-control" name="search" value="{{ search }}" 
                               placeholder="搜索设备名称或型号">
                    </div>
                </div>
                <div class="col-md-3">
                    <select class="form-select" name="type">
                        <option value="">所有类型</option>
                        <option value="sensor" {% if type == 'sensor' %}selected{% endif %}>传感器</option>
                        <option value="controller" {% if type == 'controller' %}selected{% endif %}>控制器</option>
                        <option value="actuator" {% if type == 'actuator' %}selected{% endif %}>执行器</option>
                    </select>
                </div>
                <div class="col-md-2">
                    <button type="submit" class="btn btn-primary w-100">
                        <i class="fas fa-search me-1"></i>搜索
                    </button>
                </div>
                <div class="col-md-2">
                    <a href="{{ url_for('main.equipment_list') }}" class="btn btn-secondary w-100">
                        <i class="fas fa-redo me-1"></i>重置
                    </a>
                </div>
            </form>
        </div>
    </div>

    <!-- 设备列表 -->
    <div class="row">
        {% for equipment in pagination.items %}
        <div class="col-md-6 col-lg-4 mb-4">
            <div class="card h-100">
                <div class="card-header">
                    <h5 class="mb-0">{{ equipment.name }}</h5>
                </div>
                <div class="card-body">
                    <p><strong>型号：</strong>{{ equipment.model }}</p>
                    <p><strong>类型：</strong>
                        {% if equipment.type == 'sensor' %}传感器
                        {% elif equipment.type == 'controller' %}控制器
                        {% elif equipment.type == 'actuator' %}执行器
                        {% endif %}
                    </p>
                    <p><strong>制造商：</strong>{{ equipment.manufacturer or '未知' }}</p>
                    <p><strong>单价：</strong>{{ "%.2f"|format(equipment.price or 0) }} 元</p>
                </div>
                <div class="card-footer">
                    <div class="btn-group w-100">
                        <a href="{{ url_for('main.equipment_detail', equipment_id=equipment.id) }}" 
                           class="btn btn-outline-primary">
                            <i class="fas fa-info-circle me-1"></i>详情
                        </a>
                        {% if current_user.role in ['engineer', 'manager'] %}
                        <a href="{{ url_for('main.edit_equipment', equipment_id=equipment.id) }}" 
                           class="btn btn-outline-secondary">
                            <i class="fas fa-edit me-1"></i>编辑
                        </a>
                        <button type="button" class="btn btn-outline-danger" 
                                onclick="confirmDelete({{ equipment.id }}, '{{ equipment.name }}')">
                            <i class="fas fa-trash-alt me-1"></i>删除
                        </button>
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>
        {% else %}
        <div class="col-12">
            <div class="alert alert-info">
                <i class="fas fa-info-circle me-2"></i>暂无设备数据
            </div>
        </div>
        {% endfor %}
    </div>

    <!-- 分页控件 -->
    {% if pagination.pages > 1 %}
    <nav aria-label="Page navigation" class="mt-4">
        <ul class="pagination justify-content-center">
            <li class="page-item {% if not pagination.has_prev %}disabled{% endif %}">
                <a class="page-link" href="{% if pagination.has_prev %}{{ url_for('main.equipment_list', page=pagination.prev_num, search=search, type=type) }}{% else %}#{% endif %}">
                    <i class="fas fa-chevron-left"></i>
                </a>
            </li>
            {% for page in pagination.iter_pages(left_edge=2, left_current=2, right_current=3, right_edge=2) %}
                {% if page %}
                    <li class="page-item {% if page == pagination.page %}active{% endif %}">
                        <a class="page-link" href="{{ url_for('main.equipment_list', page=page, search=search, type=type) }}">{{ page }}</a>
                    </li>
                {% else %}
                    <li class="page-item disabled"><span class="page-link">...</span></li>
                {% endif %}
            {% endfor %}
            <li class="page-item {% if not pagination.has_next %}disabled{% endif %}">
                <a class="page-link" href="{% if pagination.has_next %}{{ url_for('main.equipment_list', page=pagination.next_num, search=search, type=type) }}{% else %}#{% endif %}">
                    <i class="fas fa-chevron-right"></i>
                </a>
            </li>
        </ul>
    </nav>
    {% endif %}
</div>

<!-- 删除确认对话框 -->
<div class="modal fade" id="deleteModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content bg-light">
            <div class="modal-header bg-danger text-white">
                <h5 class="modal-title">
                    <i class="fas fa-exclamation-triangle me-2"></i>确认删除
                </h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <p class="text-dark mb-0">
                    <strong>您确定要删除以下设备吗？</strong><br>
                    设备名称：<span id="equipmentName" class="fw-bold"></span><br>
                    <small class="text-danger">
                        <i class="fas fa-exclamation-circle me-1"></i>此操作不可恢复！
                    </small>
                </p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
                    <i class="fas fa-times me-1"></i>取消
                </button>
                <form id="deleteForm" method="post" style="display: inline;">
                    <button type="submit" class="btn btn-danger">
                        <i class="fas fa-trash-alt me-1"></i>确认删除
                    </button>
                </form>
            </div>
        </div>
    </div>
</div>

<script>
function confirmDelete(equipmentId, equipmentName) {
    document.getElementById('equipmentName').textContent = equipmentName;
    document.getElementById('deleteForm').action = "{{ url_for('main.delete_equipment', equipment_id=0) }}".replace('0', equipmentId);
    new bootstrap.Modal(document.getElementById('deleteModal')).show();
}
</script>
{% endblock %} 